package com.suneee.store.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Page;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.store.bean.ViewShopStockDTO;
import com.suneee.store.dao.ViewShopStockDao;
import com.suneee.store.entity.ViewShopStock;

@Repository("viewShopStockDao")
public class ViewShopStockDaoImpl extends GenericDao<ViewShopStock, Integer>implements ViewShopStockDao {

	@Override
	public Page<Map<String, Object>> listView(ViewShopStockDTO dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = this.wrapWhereSQL(dto);
		return this.queryForList(sbf.getSql(), dto, sbf.getArgs());
	}

	private WhereClauseBuilder wrapWhereSQL(ViewShopStockDTO dto){
		Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
		List<Map<String,Object>> shopList = this.queryForList("select shop_id from t_shop_admin where admin_id = ?", adminId);
		List<Object> list = null;
		if(null != shopList && shopList.size() > 0)
		{
			list = new ArrayList<Object>();
			for(Map<String,Object> map :shopList)
			{
				list.add(map.get("shop_id"));
			}	
		}
		if(null == shopList || shopList.size() == 0)
		{
			list = new ArrayList<Object>();
			list.add(" ");
		}
		
		WhereClauseBuilder sbf =  new WhereClauseBuilder("select stock_id stockId,spu_id spuId,sku_id skuId,shop_id shopId,stock,price,shop_name shopName,brand_id brandId ,title,category_name categoryName,spec_one_value specOneValue,"
				+ "spec_two_value specTwoValue,spec_three_value specThreeValue,spec_four_value specFourValue,market_price marketPrice,enterprise_code enterpriseCode ,category_code categoryCode,channel_code channelCode,channelName,purchase_price purchasePrice from v_shop_stock where 1=1 ");
		sbf.equals("shop_id", dto.getShopId());
		sbf.in("shop_id", list);
		sbf.equals("enterprise_code", dto.getEnterpriseCode());
		sbf.contains("title", dto.getTitle());
		sbf.equals("category_code", dto.getCategoryCode());
		sbf.equals("channel_code",dto.getChannelCode());
		return sbf;
	}

}
